﻿/*
Copyright (C) 2012 Laurent Tardif (laurent.tardif@persistentsas.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using org.codeplex.TestUtils;


namespace IQToolsFonctionalTests
{
    [Binding]
    public class StepDefinition_IQ2HTML
    {
        String generatedFile = null;
        String xmlinputfile = null;


        [Given(@"I have provided a correct xml file named (.*) with (.*) listed")]
        public void GivenIHaveProvidedACorrectXmlFileNamedWithHotfixeInformationsAsked(string filename, string elements)
        {
            String dir = Directory.GetCurrentDirectory();
            xmlinputfile = Path.Combine(dir, filename);
            Console.WriteLine("Will work with configuration file " + xmlinputfile);
            Assert.IsTrue(File.Exists(xmlinputfile), "the file " + xmlinputfile + " should exist");
        }

        [Given(@"I have given an HTML outputfile named (.*)")]
        public void GivenIHaveGivenAnOutputfileNamed(string filename)
        {
            String dir = Directory.GetCurrentDirectory();
            generatedFile = Path.Combine(dir, filename);
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsFalse(File.Exists(generatedFile), "the file " + generatedFile + " should not exist");
        }

        [When(@"I run the IQ2HTML script")]
        public void WhenIRunTheIQ2HTMLScript()
        {
            Console.WriteLine("start success");
            Console.WriteLine("IQ2HTML.ps1 -filename  " + xmlinputfile + " -outputfilename " + generatedFile + " ");
            int errorCode = ShellUtils.ExecutePowershellScript("IQ2HTML.ps1", @"-filename " + xmlinputfile + " -outputfilename " + generatedFile + " ", 50000);

            Assert.AreEqual(0, errorCode, "The return code should be = 0 as all parameters are valid (directory)");
            Console.WriteLine("run success");
        }


        [Then(@"the HTML file should contain (.*) in the content")]
        public void WhenTheHTMLFileShouldContainsString(string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            Boolean check = StringUtils.CheckContains(content, sectionName);
            Assert.IsTrue(check, "File should contain " + sectionName );
        }

        [Then(@"the HTML file should not contain (.*) in the content")]
        public void WhenTheHTMLFileShouldNotContainsString(string sectionName)
        {
            Console.WriteLine("Will work with result file " + generatedFile);
            Assert.IsTrue(File.Exists(generatedFile), "the file " + generatedFile + " should exist");
            String content = File.ReadAllText(generatedFile);
            Boolean check = StringUtils.CheckDoNotContains(content, sectionName);
            Assert.IsTrue(check, "File contains " + sectionName );
        }


    }
}
